function reload( filename )
    --卸载旧文件, 只进行重新加载, 不做递归子文件
    if package[filename] then 
       package.loaded[filename] = nil
    end
    require(filename)
end

function alert(msg,title)
    dv.LuaSystem:GetInstance():alert(msg,title)
end

function MD5String(szString)
   return  dv.LuaSystem:GetInstance():MD5String(szString)
end

function utf8togbk(src)
   return dv.LuaSystem:GetInstance():utf8togbk(src)
end

function gbktoutf8(src)
    return dv.LuaSystem:GetInstance():gbktoutf8(src)
end
    
function setTimer(uElapse,lpTimerFunc,bPaused) -- uElapse 为毫秒
   local scheduler = cc.Director:getInstance():getScheduler()
   local nIDEvent = scheduler:scheduleScriptFunc(lpTimerFunc, uElapse/1000, bPaused)
   return  nIDEvent 
end

function killTimer( nIDEvent ) 
    cc.Director:getInstance():getScheduler():unscheduleScriptEntry(nIDEvent) 
end

function removeAllChild(node)
    if node  then
        for k, item in ipairs(node:getChildren()) do
            node:removeChild(item,true)
        end
    end
end
--create sprite from sprite frame
function SpriteWithFrame(file)
    return cc.Sprite:createWithSpriteFrameName(file)
end

--create sprite frame for file
function LoadSpriteFrame(file)
    return cc.SpriteFrameCache:getInstance():getSpriteFrame(file)
end

function LocalizedString(key)
   
    local str = LocalStr["" .. key] or "error"
    return str
end

function LanguageType()
    local languageType = cc.Application:getInstance():getCurrentLanguage()
    if languageType == cc.LANGUAGE_CHINESE then
        return true;
    end
    return false;
end

function getDeviceCachesPath()
    return cc.FileUtils:getInstance():getWritablePath() 
end

function cclog(...)
   -- if ISLOG then
       print(string.format(...))
  --  end
end

--判断两点坐标是否重叠
function isEqualToPoint(pos1,pos2)
    return pos1.x == pos2.x and pos1.y == pos2.y;
end
 
function removeEventListener(eventname)
    cc.Director:getInstance():getEventDispatcher():removeCustomEventListeners(eventname);
end

function addEventListenerCustom(eventName,hd,obj)
    if not eventName or #tostring(eventName) < 1 then
        local strname = nil
        return
    end
    local listener = cc.EventListenerCustom:create(eventName, hd)
    cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,obj)
end

function dispatchEventCustom(eventName,values)
    if not eventName or #tostring(eventName) < 1 then
        local strname = nil
    	return
    end
    local event = cc.EventCustom:new(eventName)
    event.data = values
    cc.Director:getInstance():getEventDispatcher():dispatchEvent(event)
end

function addEventListenerTouchOneByOne(obj , hdb , hdm , hde , isSwall)
    local listen = cc.EventListenerTouchOneByOne:create()
    listen:registerScriptHandler(hdb,cc.Handler.EVENT_TOUCH_BEGAN)
    listen:registerScriptHandler(hdm,cc.Handler.EVENT_TOUCH_MOVED)
    listen:registerScriptHandler(hde,cc.Handler.EVENT_TOUCH_ENDED)
    if not (isSwall == nil) then
        listen:setSwallowTouches(isSwall)
    end
    local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listen,obj)
    return listen
end

--四舍五入取整
function getNearIntFromFloat(value)
    local floor = math.floor(value)
    local ceil = math.ceil(value)
    if value-floor > ceil-value then
        return ceil
    else
        return floor
    end
end

--判断点在有向直线的左侧还是右侧  返回值:-1: 点在线段左侧; 0: 点在线段上; 1: 点在线段右侧
-- 基于cocos2dx坐标系与如flash坐标系y轴相反，-1与1倒转过来.
function pointAtLineLeftRight(startPos, endPos ,testPos)
    local startPosx = startPos.x-testPos.x
    local startPosy = startPos.y - testPos.y
    local endPosx = endPos.x-testPos.x
    local endPosy = endPos.y-testPos.y
    local ret = startPosx * endPosy - startPosy * endPosx
    if ret == 0 then
        return 0
    elseif ret > 0 then
        return -1
    else
        return 1
    end
end
--判断两条线段是否相交
function isTowLineIntercet(startPos1, endPos1 , startPos2, endPos2)
    local nline1Start = pointAtLineLeftRight(startPos2 ,endPos2 ,startPos1)
    local nline1End = pointAtLineLeftRight(startPos2 ,endPos2 ,endPos1)
    if (nline1Start * nline1End) > 0 then
        return false
    end

    local nline2Start = pointAtLineLeftRight(startPos1 , endPos1 ,startPos2)
    local nline2End = pointAtLineLeftRight(startPos1 , endPos1 ,endPos2)
    if (nline2Start*nline2End) > 0 then
        return false
    end
    return true
end
--点在矩形里面
function isPointInRect(poi,rect)
    if poi.x>rect.x and poi.x<rect.x+rect.width and poi.y < rect.y and poi.y>rect.y-rect.height then
        return true
    end
    return false
end
--判断直线与矩形是否相交  直线在矩形内也是相交
function isLineIntersetRect(startPos, endPos , rect)
    --有一点在矩形内
    if isPointInRect(startPos, rect) or isPointInRect(endPos, rect) then
        return true
    end
    if isTowLineIntercet(startPos,endPos,cc.p(rect.x,rect.y),cc.p(rect.x+rect.width,rect.y) )
        or isTowLineIntercet(startPos,endPos,cc.p(rect.x,rect.y),cc.p(rect.x,rect.y-rect.height) )
        or isTowLineIntercet(startPos,endPos,cc.p(rect.x,rect.y-rect.height),cc.p(rect.x+rect.width,rect.y-rect.height) )
        or isTowLineIntercet(startPos,endPos,cc.p(rect.x+rect.width,rect.y),cc.p(rect.x+rect.width,rect.y-rect.height) ) then
        return true
    end
    return false
end

--判断矩形与矩形是否相交
function isRectIntersetRect(rect1, rect2)
    --    if isPointInRect(cc.p(rect1.x,rect1.y),rect2) or isPointInRect(cc.p(rect1.x+rect1.width,rect1.y),rect2) or
    --        isPointInRect(cc.p(rect1.x,rect1.y-rect1.height),rect2) or isPointInRect(cc.p(rect1.x+rect1.width,rect1.y-rect1.height),rect2) then
    --        return true
    --    end
    --
    --    if isPointInRect(cc.p(rect2.x,rect2.y),rect1) or isPointInRect(cc.p(rect2.x+rect2.width,rect2.y),rect1) or
    --        isPointInRect(cc.p(rect2.x,rect2.y-rect2.height),rect1) or isPointInRect(cc.p(rect2.x+rect2.width,rect2.y-rect2.height),rect1) then
    --        return true
    --    end

    if math.abs(rect1.x + rect1.width/2 - (rect2.x + rect2.width/2)) <= (rect1.width/2 + rect2.width/2) and
        math.abs(rect1.y - rect1.height/2 - (rect2.y - rect2.height/2)) <= (rect1.height/2 + rect2.height/2) then
        return true
    end

    return false
end

function Split(szFullString, szSeparator)
    local nFindStartIndex = 1
    local nSplitIndex = 1
    local nSplitArray = {}
    while true do
        local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
        if not nFindLastIndex then
            nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
            break
        end
        nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
        nFindStartIndex = nFindLastIndex + string.len(szSeparator)
        nSplitIndex = nSplitIndex + 1
    end
    return nSplitArray
end
 
function cc_shake(node ,duration , strengthx, strengthy , times , isRevert)
    local actions = {}

    local index = 0
    local initposx = node:getPositionX()
    local initposy = node:getPositionY()
    
    local shaketimes = times
    if not shaketimes then
    	shaketimes = 10
    end
    local dur = duration/shaketimes
    for var=1, shaketimes, 1 do
        local offx = strengthx * (shaketimes - var)/shaketimes
        local offy = strengthy * (shaketimes - var)/shaketimes
        if isRevert then
            offx = strengthx * var/shaketimes
            offy = strengthy * var/shaketimes
        end
    	
        local mb = cc.MoveBy:create(dur/4,cc.p(-offx, -offy))
        local mb1 = cc.MoveBy:create(dur/4,cc.p(offx, offy))
        local mb2 = cc.MoveBy:create(dur/4,cc.p(offx, offy))
        local mb3 = cc.MoveBy:create(dur/4,cc.p(-offx, -offy))
        local rp = cc.Repeat:create(cc.Sequence:create(mb,mb1,mb2,mb3),1)
        table.insert(actions,rp)
    end
    
    if #actions > 0 then
        node:runAction(cc.Sequence:create(actions))
    end
end
function screenShake(duration , strengthx, strengthy , times)
    local scene = cc.Director:getInstance():getRunningScene()
    cc_shake(scene, duration , strengthx, strengthy , times)
end

function getStandardTypeForScend(count)
    if not count or count == 0 then
        return "00:00"
    end

    local h = 0
    local m = 0
    local s = 0
    local rt = ""
    if count >= 3600 then
        h = math.floor(count/3600)
        count = count%3600
        if h<10 then
            h="0"..h
        end
        rt = rt..h..":"
    end
    
    if count >= 60 then
        m = math.floor(count/60)
        count = count%60
        if m<10 then
            m="0"..m
        end
        rt = rt..m..":"
    else
        rt = rt.."00:"
    end
    
    if count<10 then
        count="0"..count
    end
    rt = rt..count

    return rt
end

function cc_getSecondaryTitle(str)
  local introduce1 = cc.Label:createWithSystemFont(str, DEFAULT_FONT_BOLD, 36, cc.size(500, 0), 1, 1)
  introduce1:setColor(cc.c3b(160, 160, 160))
  return introduce1
end

function cc_getSuperMethod(table, methodName)
  local mt = getmetatable(table)
  local method
  while mt and not method do
    method = mt[methodName]
    if not method then
      local index = mt.__index
      if index and type(index) == "function" then
        method = index(mt, methodName)
      elseif index and type(index) == "table" then
        method = index[methodName]
      end
    end
    mt = getmetatable(mt)
  end
  return method
end

function cc_getCardNumber(value)
  local bs = bit:band(bit:d2b(value), bit:d2b(15))
  return tonumber(bit:b2d(bs))
end

function cc_getCardType(value)
  local bs = bit:band(bit:d2b(value), bit:d2b(240))
  bs = bit:bright(bs, 4)
  return tonumber(bit:b2d(bs))
end

function cc_getChipsRound(value)
  local tv = ""
  if value < 0 then
    tv = "-"
    value = math.abs(value)
  end
  local tmp = value
  if value >= 10000000000 then
    tmp = math.floor(value / 1000000000) .. "\229\132\132"
  elseif value >= 100000000 then
    tmp = math.floor(value / 10000) .. "\232\144\172"
  elseif value >= 10000000 then
    tmp = math.floor(value / 10000) .. "\232\144\172"
  elseif value >= 1000000 then
    tmp = math.floor(value / 10000) .. "\232\144\172"
  elseif value >= 100000 then
    tmp = math.floor(value / 10000) .. "\232\144\172"
  end
  return tv .. tmp
end

function cc_getGoldRound(value)
  local tmp = value
  if value >= 10000000000 then
    tmp = math.floor(value / 1000000000)
  elseif value >= 100000000 then
    tmp = math.floor(value / 10000)
  elseif value >= 10000000 then
    tmp = math.floor(value / 10000)
  elseif value >= 1000000 then
    tmp = math.floor(value / 10000)
  elseif value >= 100000 then
    tmp = math.floor(value / 10000)
  end
  return tmp
end

function cc_getGoldAddStep(value)
  local tmp = 1
  if value > 10000000000 then
    tmp = 10000000000
  elseif value > 1000000000 then
    tmp = 1000000000
  elseif value > 100000000 then
    tmp = 100000000
  elseif value > 10000000 then
    tmp = 10000000
  elseif value > 1000000 then
    tmp = 1000000
  elseif value > 100000 then
    tmp = 100000
  elseif value > 10000 then
    tmp = 10000
  elseif value > 100 then
    tmp = 100
  elseif value > 10 then
    tmp = 10
  end
  return tmp
end



function getTimeByServiceTypeDate(date)
  if date.wYear == 0 then
    return os.time()
  end
  local t = os.time({
    year = date.wYear,
    month = date.wMonth,
    day = date.wDay,
    hour = date.wHour,
    min = date.wMinute,
    sec = date.wSecond
  })
  return t
end


